﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ModBot.ConfigBuilder.Providers
{
    public class PullSettingsProvider
    {
        private ModBotSettingsProvider parentProvider;

        #region Public Properties
        /// <summary>
        /// Gets or sets the number of seconds to wait between 
        /// running the path file or pulling a mob at camp.
        /// </summary>
        public int APCheckTime { get; set; }

        /// <summary>
        /// Gets or sets the radius at which a bot will engage a mob.
        /// </summary>
        public int APRadius { get; set; }

        /// <summary>
        /// Gets or sets the maximum number of mobs near the target mob
        /// before aborting pull.
        /// </summary>
        public int APMobMax { get; set; }

        /// <summary>
        /// Gets or sets the reference to the script used when actually
        /// performing the pull.
        /// </summary>
        public string APScript { get; set; }

        /// <summary>
        /// Gets or sets the MBWayPnt.ini path section to use for waypoint
        /// traversing.
        /// </summary>
        public string APPath { get; set; }

        /// <summary>
        /// Gets or sets the script code to execute before running the pull script. 
        /// Note : Uses ModBot script code (same as MQ2 without "$").        
        /// </summary>        
        public string APBefore { get; set; }

        /// <summary>
        /// Gets or sets the script code to execute after leaving pull script.
        /// Note : Uses ModBot script code (same as MQ2 without "$").
        /// </summary>
        public string APAfter { get; set; }


        /// <summary>
        /// Gets or sets the cannel to announce in or if left
        /// blank disables annoucement.
        /// </summary>
        public string APAnnounce { get; set; }        

        #endregion

        private PullSettingsProvider()
        {
            this.APCheckTime = Defaults.Default_PullSettings_APCheckTime;
            this.APRadius = Defaults.Default_PullSettings_APRadius;
            this.APMobMax = Defaults.Default_PullSettings_APMobMax;
            this.APScript = Defaults.Default_PullSettings_APScript;
            this.APPath = Defaults.Default_PullSettings_APPath;
            this.APBefore = Defaults.Default_PullSettings_APBefore;
            this.APAfter = Defaults.Default_PullSettings_APAfter;
            this.APAnnounce = Defaults.Default_PullSettings_APAnnounce;
        }

        public PullSettingsProvider(ModBotSettingsProvider parentProvider)
        {
            this.parentProvider = parentProvider;
        }

        public override string ToString()
        {
            #region Format String
            string formatString =
                "[AdvPull]\r\n" +
                "APCheckTime={0}\r\n" +
                "APRadius={1}\r\n" +
                "APMobMax={2}\r\n" +
                "APScript={3}\r\n" +
                "APPath={4}\r\n" +
                "APBefore={5}\r\n" +
                "APAfter={6}\r\n" + 
                "APAnnounce{7}\r\n";

            #endregion

            return string.Format(formatString,
                APCheckTime,
                APRadius,
                APMobMax,
                APScript,
                APPath,
                APBefore,
                APAfter,
                APAnnounce);
        }

        public static PullSettingsProvider LoadFromINI(ModBotSettingsProvider settingsProvider, string filepath)
        {
            PullSettingsProvider provider = new PullSettingsProvider(settingsProvider);

            try
            {
                provider.APCheckTime = SharedLib.ConvertToInt32(IniFileProvider.ReadValue(filepath, Keys.PullSettings_Section_Key, Keys.PullSettings_Key_APCheckTime), Defaults.Default_PullSettings_APCheckTime);
                provider.APRadius = SharedLib.ConvertToInt32(IniFileProvider.ReadValue(filepath, Keys.PullSettings_Section_Key, Keys.PullSettings_Key_APRadius), Defaults.Default_PullSettings_APRadius);
                provider.APMobMax = SharedLib.ConvertToInt32(IniFileProvider.ReadValue(filepath, Keys.PullSettings_Section_Key, Keys.PullSettings_Key_APMobMax), Defaults.Default_PullSettings_APMobMax);
                provider.APScript = IniFileProvider.ReadValue(filepath, Keys.PullSettings_Section_Key, Keys.PullSettings_Key_APScript);
                provider.APPath = IniFileProvider.ReadValue(filepath, Keys.PullSettings_Section_Key, Keys.PullSettings_Key_APPath);
                provider.APBefore = IniFileProvider.ReadValue(filepath, Keys.PullSettings_Section_Key, Keys.PullSettings_Key_APBefore);
                provider.APAfter = IniFileProvider.ReadValue(filepath, Keys.PullSettings_Section_Key, Keys.PullSettings_Key_APAfter);
                provider.APAnnounce = IniFileProvider.ReadValue(filepath, Keys.PullSettings_Section_Key, Keys.PullSettings_Key_APAnnounce);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return provider;
        }

        public static void SaveToIni(PullSettingsProvider provider, string filepath)
        {
            IniFileProvider.WriteValue(filepath, Keys.PullSettings_Section_Key, Keys.PullSettings_Key_APCheckTime, Convert.ToString(provider.APCheckTime));
            IniFileProvider.WriteValue(filepath, Keys.PullSettings_Section_Key, Keys.PullSettings_Key_APRadius, Convert.ToString(provider.APRadius));
            IniFileProvider.WriteValue(filepath, Keys.PullSettings_Section_Key, Keys.PullSettings_Key_APMobMax, Convert.ToString(provider.APMobMax));
            IniFileProvider.WriteValue(filepath, Keys.PullSettings_Section_Key, Keys.PullSettings_Key_APScript, Convert.ToString(provider.APScript));
            IniFileProvider.WriteValue(filepath, Keys.PullSettings_Section_Key, Keys.PullSettings_Key_APPath, Convert.ToString(provider.APPath));
            IniFileProvider.WriteValue(filepath, Keys.PullSettings_Section_Key, Keys.PullSettings_Key_APBefore, Convert.ToString(provider.APBefore));
            IniFileProvider.WriteValue(filepath, Keys.PullSettings_Section_Key, Keys.PullSettings_Key_APAfter, Convert.ToString(provider.APAfter));
            IniFileProvider.WriteValue(filepath, Keys.PullSettings_Section_Key, Keys.PullSettings_Key_APAnnounce, Convert.ToString(provider.APAnnounce));
        }
    }

}
